package org.dromara.product.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.product.domain.bo.KyProductsBo;
import org.dromara.product.domain.vo.KyProductsVo;
import org.dromara.product.domain.KyProducts;
import org.dromara.product.mapper.KyProductsMapper;
import org.dromara.product.service.IKyProductsService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 商品Service业务层处理
 *
 * @author kuyu
 * @date 2023-12-27
 */
@RequiredArgsConstructor
@Service
public class KyProductsServiceImpl implements IKyProductsService {

    private final KyProductsMapper baseMapper;

    /**
     * 查询商品
     */
    @Override
    public KyProductsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询商品列表
     */
    @Override
    public TableDataInfo<KyProductsVo> queryPageList(KyProductsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KyProducts> lqw = buildQueryWrapper(bo);
        Page<KyProductsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询商品列表
     */
    @Override
    public List<KyProductsVo> queryList(KyProductsBo bo) {
        LambdaQueryWrapper<KyProducts> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KyProducts> buildQueryWrapper(KyProductsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<KyProducts> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), KyProducts::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), KyProducts::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getUnit()), KyProducts::getUnit, bo.getUnit());
        lqw.eq(StringUtils.isNotBlank(bo.getBrand()), KyProducts::getBrand, bo.getBrand());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplyId()), KyProducts::getSupplyId, bo.getSupplyId());
        lqw.eq(StringUtils.isNotBlank(bo.getBarcode()), KyProducts::getBarcode, bo.getBarcode());
        lqw.eq(bo.getNumber() != null, KyProducts::getNumber, bo.getNumber());
        lqw.eq(bo.getLowerNumber() != null, KyProducts::getLowerNumber, bo.getLowerNumber());
        lqw.eq(bo.getUpperNumber() != null, KyProducts::getUpperNumber, bo.getUpperNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getAlarmStatus()), KyProducts::getAlarmStatus, bo.getAlarmStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getQrcode()), KyProducts::getQrcode, bo.getQrcode());
        return lqw;
    }

    /**
     * 新增商品
     */
    @Override
    public Boolean insertByBo(KyProductsBo bo) {
        KyProducts add = MapstructUtils.convert(bo, KyProducts.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改商品
     */
    @Override
    public Boolean updateByBo(KyProductsBo bo) {
        KyProducts update = MapstructUtils.convert(bo, KyProducts.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KyProducts entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
